package com.lxlg.lxlg_order.app.controller;


import com.alipay.api.AlipayApiException;
import com.lxlg.core.utils.Result;
import com.lxlg.lxlg_order.client.VipRechargeClient;
import com.lxlg.lxlg_order.client.param.PayRechargeOrderForm;
import com.lxlg.lxlg_order.client.param.Score2MoneyForm;
import com.lxlg.lxlg_order.client.param.UserRechargeForm;
import com.lxlg.lxlg_order.client.result.RechargeOrderInfo;
import com.lxlg.lxlg_order.common.WebUtils;
import com.lxlg.lxlg_order.service.service.RechargeVipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/infra/recharge-vip")
@Api("会员充值")
public class RechargeVipController implements VipRechargeClient {
    @Autowired
    private RechargeVipService rechargeVipService;

    @Override
    @PostMapping("newRechargeOrder")
    @ApiOperation("创建支付订单，只能用钱充值")
    public Result<RechargeOrderInfo> newRechargeOrder(@Valid @RequestBody UserRechargeForm userRechargeForm) {
        return rechargeVipService.addRechargeOrder(userRechargeForm);
    }

    @Override
    @PostMapping("payRechargeOrder")
    @ApiOperation("支付订单")
    public void payRechargeOrder(@Valid @RequestBody PayRechargeOrderForm payRechargeOrderForm) {
        HttpServletResponse resp = WebUtils.getResponse();
        try {
            rechargeVipService.DoPay4RechargeOrder(payRechargeOrderForm,resp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    //提现
    @PostMapping("getRealMoney")
    public Result<String> getRealMoney(@Valid @RequestBody Score2MoneyForm score2MoneyForm)  {
        try {
            return rechargeVipService.doScore2Money(score2MoneyForm);
        } catch (AlipayApiException e) {
            throw new ArithmeticException();
        }
    }


}

